package output;



import instance.Instance;
import instance.InstanceEj2;

import java.io.BufferedWriter;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.LinkedList;

import problem.ej2.Ejes_strings;




public class Problem2Output implements Output{
	private static String outputFile;

	public Problem2Output(String nombArch){
		
	  outputFile =nombArch;
	}
	public void writeInstance(LinkedList<InstanceEj2> lista_insts) throws Exception {
		/*||CUIDADO||:Usar después de generar el tipo Problem2Output con el constructor que cambia el outputfile*/
		BufferedWriter bw = new BufferedWriter(new PrintWriter( Problem2Output.outputFile) );
		
		Iterator<InstanceEj2> it = lista_insts.iterator();
		while(it.hasNext()){
			InstanceEj2 inst = it.next();
			String pyq = inst.getP_y_q().inv_1 + " " + inst.getP_y_q().inv_2;  
			bw.write(pyq); bw.newLine();
			LinkedList<Ejes_strings> ejes = inst.getInstancia();
			Iterator<Ejes_strings> it2 = ejes.iterator();
			while(it2.hasNext()){
				Ejes_strings eje = it2.next();
				String st_eje = eje.inv_1 + " " + eje.inv_2 + ";";
				bw.write(st_eje);
			}
			if(it.hasNext()) bw.newLine();
		}
		bw.close();
	}
	
	public void writeTestRend(LinkedList<String> strings_salida) throws Exception {
		/*||CUIDADO||:Usar después de generar el tipo Problem2Output con el constructor que cambia el outputfile*/
		BufferedWriter bw = new BufferedWriter(new PrintWriter( Problem2Output.outputFile) );
		
		Iterator<String> it_res = strings_salida.iterator();
		while(it_res.hasNext()){
			String st = it_res.next();
			bw.write(st);
			if(it_res.hasNext()) bw.newLine();
		}
		bw.close();
	}
	
	@Override
	public void writeFile(String soluciones) throws Exception {
		
		BufferedWriter bw = new BufferedWriter(new PrintWriter(
				Problem2Output.outputFile));

		bw.write(soluciones);
        bw.newLine();
        bw.close();
	}
	public void escribirSolucion(LinkedList<Integer> soluciones) throws Exception {
		
		BufferedWriter bw = new BufferedWriter(new PrintWriter(
				Problem2Output.outputFile));
		
		Iterator<Integer> it_res = soluciones.iterator();
		while(it_res.hasNext()){
			String st = it_res.next().toString();
			bw.write(st);
			if(it_res.hasNext()) bw.newLine();
		}
		bw.close();
	}
}

